package fr.babos.test.navigation 
{
	import com.bit101.components.InputText;
	import com.demonsters.debugger.MonsterDebugger;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.utils.setTimeout;
	import fr.babos.core.log.ExternalInterfaceLogger;
	import fr.babos.core.log.Log;
	import fr.babos.core.log.MonsterLogger;
	import fr.babos.core.log.TraceLogger;
	import fr.babos.navigation.routes.RouteItem;
	import fr.babos.navigation.routes.Router;
	
	/**
	 * @author ZoulouX
	 */
	public class NavigationTest extends Sprite 
	{
		/**
		 * Les routes
		 */
		protected var _route1					:RouteItem;
		protected var _route2					:RouteItem;
		protected var _route3					:RouteItem;
		
		/**
		 * Constructeur
		 */
		public function NavigationTest ()
		{
			// Initialiser le logger
			Log.addLogger(new TraceLogger());
			Log.addLogger(new MonsterLogger(this));
			Log.addLogger(new ExternalInterfaceLogger());
			
			// Créer des routes
			_route1 = new RouteItem("Pages.index", "/test/index.html");
			_route2 = new RouteItem("Pages.news", "/test/{param1:alphanum}.html");
			_route3 = new RouteItem("Test.other", "/test/{param1:alphanum}-{param2:num}.html");
			
			// Afficher les routes
			Log.log(_route1);
			Log.log(_route2);
			Log.log(_route3);
			
			//setTimeout(testBasicRoute, 200);
			setTimeout(testBasicRouter, 200);
		}
		
		/**
		 * Tester les fonctions basiques d'une route
		 */
		public function testBasicRoute ():void
		{
			// Vérifier des commandes
			Log.log(
				"\nroute1.checkCommand\n",
				_route1.checkCommand("Pages.index"),
				"-",
				_route1.checkCommand("Pages"),
				_route1.checkCommand("Pages.index", {
					toto: "tutu"
				})
			);
			Log.log(
				"\nroute2.checkCommand\n",
				_route2.checkCommand("Pages.news", {
					param1: "tutu"
				}),
				"-",
				_route2.checkCommand("Pages.news"),
				_route2.checkCommand("Pages"),
				_route2.checkCommand("Pages.news", {
					param1: "tutu",
					param2: "tutu"
				}),
				_route2.checkCommand("Pages.news", {
					param2: "tutu"
				})
			);
			Log.log(
				"\nroute3.checkCommand\n",
				_route3.checkCommand("Test.other", {
					param1: "tutu",
					param2: 5
				}),
				"-",
				_route3.checkCommand("Test.other", {
					param1: "tutu",
					param2: "tutu"
				}),
				_route3.checkCommand("Test.other", {
					param1: "tutu",
					param2: 5,
					param3: "test"
				}),
				_route3.checkCommand("Test.other", {
					param2: 5,
					param3: "test"
				})
			);
			
			// Vérifier des routes
			Log.log(
				"\nroute1.checkURL\n",
				_route1.checkURL("/test/index.html"),
				_route1.checkURL("/tesT/index.HTML"),
				"-",
				_route1.checkURL("/test/index.html?"),
				_route1.checkURL("/test/index.html(.)*"),
				_route1.checkURL("/test/index3html"),
				_route1.checkURL("TEST/index.html")
			);
			Log.log(
				"\nroute2.checkURL\n",
				_route2.checkURL("/test/tata-tutu-toto.html"),
				_route2.checkURL("/test/5.html"),
				_route2.checkURL("/test/-.html"),
				_route2.checkURL("/test/_.html"),
				_route2.checkURL("/test/-_-_-_-.html"),
				"-",
				_route2.checkURL("/test/.html"),
				_route2.checkURL("/test/tata/tutu/toto.html"),
				_route2.checkURL("/test/ .html"),
				_route2.checkURL("/test/%20.html"),
				_route2.checkURL("/test/..html"),
				_route2.checkURL("/test/....html"),
				_route2.checkURL("/test/().html"),
				_route2.checkURL("/test/{}.html"),
				_route2.checkURL("/test/#.html"),
				_route2.checkURL("/test/'.html")
			);
			
			// Récupérer les paramètres depuis une URL
			Log.log(
				"\ngetParamsFromURL\n",
				_route2.getParamsFromURL("/test/tata-tutu-toto.html"),
				_route3.getParamsFromURL("/test/tata-tutu-toto.html"),
				_route3.getParamsFromURL("/test/tata-tutu-toto-5.html"),
				_route3.getParamsFromURL("/test/tata-tutu-10-5.html"),
				_route3.getParamsFromURL("/test/10-10-10-5.html"),
				_route3.getParamsFromURL("/test/HOBAH-CA-ALORS-3546354654654651416540.html")
			);
			
			// Récupérer l'URL depuis les paramètres
			Log.log(
				_route1.getURLFromParams( {
					param1: "test",
					toto: "tutu"
				}),
				_route2.getURLFromParams( {
					tutu: "test",
					toto: "tutu"
				}),
				_route2.getURLFromParams( {
					param1: "test",
					toto: "tutu"
				}),
				_route2.getURLFromParams( {
					param1: "test"
				}),
				_route3.getURLFromParams( {
					param1: "test",
					param2: "tutu"
				}),
				_route3.getURLFromParams( {
					param1: "test",
					param2: 5
				}),
				_route3.getURLFromParams( {
					param1: 5,
					param2: 5
				})
			);
		}
		
		/**
		 * Tester le router
		 */
		public function testBasicRouter ():void
		{
			// Configurer le stage
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			// Créer le router
			var router:Router = new Router();
			
			// Ajouter les routes
			//router.routes.addAll([_route1, _route2, _route3]);
			
			router.loadRoutes("xml/routes.xml");
			
			// Démarrer le router
			router.start();
			
			var addressField:InputText = new InputText(this, 10, 10);
			addressField.width = stage.stageWidth - 20;
			
			function addressKeyDownHandler (event:KeyboardEvent):void
			{
				if (event.keyCode == Keyboard.ENTER)
					router.url = addressField.text;
			}
			
			addressField.addEventListener(KeyboardEvent.KEY_DOWN, addressKeyDownHandler);
			
			
			// Ecouter les changements sur le routeur
			router.onRouteChange.add(function ():void
			{
				Log.log("onRouteChange", router.currentRoute);
			});
			router.onRouteError.add(function ():void
			{
				Log.log("onRouteError", router.currentRoute);
			});
			router.onURLChange.add(function ():void
			{
				Log.log("onURLChange");
				
				addressField.text = router.url;
			});
		}
		
		/**
		 * 
		 */
		public function testRemoteRouter ():void
		{
			
		}
	}
}